//
// C++ Implementation: log
//
// Description:
//
//
// Author: Maciej Skrzypkowski <>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "Log.h"

Log::Log()
{
	if (OVERWRITE_FILE) {
		fs.open(FILENAME.c_str(), ios::trunc);
		fs.close();
	}
}


Log::~Log()
{
}


void Log::info(string s)
{
	string s2("[INFO] ["+ getDateTime() +"]\t"+s+"\n");
	write(s2);
}

void Log::debug(string s)
{
	string s2("[DEBUG] ["+ getDateTime() +"]\t"+s+"\n");
	write(s2);
}

void Log::warn(string s)
{
	string s2("[WARN] ["+ getDateTime() +"]\t"+s+"\n");
	write(s2);

}

void Log::error(string s)
{
	string s2("[ERROR] ["+ getDateTime() +"]\t"+s+"\n");
	write(s2);
}

void Log::write(string s)
{
	fs.open(FILENAME.c_str(), ios::app);
	fs << s;
	fs.close();
	if (PUT_ON_SCREEN)
	{
		cout<<s;
		flush(cout);
	}
}

string Log::getDateTime()
{
	time_t rawtime;
	struct tm * timeinfo;
	char buffer [40];

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );

	strftime (buffer,40,"%Y-%m-%d %H:%M:%S",timeinfo);
	string s(buffer);

	return s;
}

Log *Log::inst = 0;

Log *Log::instance()
{
	if (!inst)
		inst = new Log();
	return inst;
}

